package com.itheima.controller.admin;

import com.itheima.common.entity.Result;
import com.itheima.service.MemberService;
import com.itheima.service.ReportService;
import com.itheima.service.SetmealService;
import com.itheima.vo.BusinessReportDataVO;
import com.itheima.vo.MemberReportVO;
import com.itheima.vo.SetmealReportVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * 统计接口
 */
@RestController
@RequestMapping("/report")
@Slf4j
public class ReportController {
    @Autowired
    private MemberService memberService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private ReportService reportService;

    /**
     * 获取会员数量统计数据
     * @return
     */
    @GetMapping("/getMemberReport")
    public Result<MemberReportVO> getMemberReport() {
        log.info("获取会员数量统计数据");
        MemberReportVO memberReportVO = memberService.getMemberReport();
        return Result.success(memberReportVO,"获取会员统计数据成功");
    }

    /**
     * 获取套餐占比统计数据
     * @return
     */
    @GetMapping("/getSetmealReport")
    public Result<SetmealReportVO> getSetmealReport() {
        log.info("获取套餐占比统计数据");
        SetmealReportVO setmealReportVO = setmealService.getSetmealReport();

        return Result.success(setmealReportVO,"获取套餐统计数据成功");
    }

    /**
     *获取运营统计数据
     * @return
     */
    @GetMapping("/getBusinessReportData")
    public  Result<BusinessReportDataVO> getBusinessReportData() {
        log.info("获取运营统计数据");
        BusinessReportDataVO businessReportDataVO = reportService.getBusinessReportData();

        return Result.success(businessReportDataVO,"获取运营统计数据成功");
    }
    /**
     * 导出运营文件
     */
    @GetMapping("/exportBusinessReport")
    public Result exportBusinessReport(HttpServletResponse response) {
        log.info("导出运营数据到Excel并提供客户端下载");
        try {
            reportService.exportBusinessReport(response);
            return null;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  Result.error( "导出运营数据到Excel并提供客户端下载失败");
    }
}
